/**
 * 银联支付前台通知地址
 * @see [https://open.unionpay.com/tjweb/acproduct/APIList?proId=1]
 */
import { ICommonParams } from '@feidao/service/interfaces';
import log from '@feidao/web/atom/nodejs/logger/log';

interface IUnionJSON {
	queryId: string; // 查询流水号	AN20..21	无	M-必填	消费交易的流水号，供后续查询用
	signature: string; // 签名	ANS1..1024	0	M-必填
	signMethod: string; // 签名方法	N2	无	M-必填
	respCode: string; // 应答码	AN2	无	M-必填
	respMsg: string; // 应答信息	ANS1..256	无	M-必填
	signPubKeyCert: string; // 签名公钥证书	AN2048	无	C-按条件必填	使用RSA签名方式时必选，此域填写银联签名公钥证书。
	tn: string; // 银联受理订单号	N21	无	C - 按条件必填	商户推送订单后银联移动支付系统返回该流水号，商户调用支付控件时使用
	accNo: string; // 账号	AN1..1024	无	C - 按条件必填	根据商户配置返回
	payType: string; // 支付方式	N4	无	C - 按条件必填	根据商户配置返回
	payCardType: string; // 支付卡类型	N2	无	C - 按条件必填	根据商户配置返回
	version: string; // 版本号	NS5	无	R - 需要返回
	encoding: string; // 编码方式	ANS1..20	UTF - 8	R - 需要返回
	bizType: string; // 产品类型	N6	无	R - 需要返回
	txnTime: string; // 订单发送时间	YYYYMMDDhhmmss	无	R - 需要返回
	currencyCode: string; // 交易币种	AN3	无	R - 需要返回	默认为156
	txnAmt: string; // 交易金额	N1..12	无	R - 需要返回
	txnType: string; // 交易类型	N2	无	R - 需要返回
	txnSubType: string; // 交易子类	N2	无	R - 需要返回
	accessType: string; // 接入类型	N1	无	R - 需要返回0：商户直连接入1：收单机构接2：平台商户接入

	reqReserved: string; // 请求方保留域	ANS1..1024	无	R - 需要返回
	merId: string; // 商户代码	AN15	无	R - 需要返回
	orderId: string; // 商户订单号	AN8..40	无	R - 需要返回 商户订单号，不能含“-”或“_”;商户自定义，同一交易日期内不可重复;商户代码merId、商户订单号orderId、订单发送时间txnTime三要素唯一确定一笔交易。

	reserved: string; // 保留域	ANS1..2048	无	O - 选填	查看详情
}

// 支付处理
async function pay_process(data: IUnionJSON, _spaceid: string) {
	// todo 做支付处理
	return true;
}

export default async function atom(msg: ICommonParams, action_id: string, session_id: string, _spaceid: string) {
	log('银联支付前台通知服务开始');
	// {"body":{"accNo":"6226********0048","accessType":"0","bizType":"000201","currencyCode":"156","encoding":"UTF-8","exchangeRate":"0","merId":"777290058165389","orderId":"1550196180907","queryId":"641902151003000606268","respCode":"00","respMsg":"success","settleAmt":"1","settleCurrencyCode":"156","settleDate":"0215","signMethod":"01","signPubKeyCert":"-----BEGIN CERTIFICATE-----\r\nMIIEQzCCAyugAwIBAgIFEBJJZVgwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UEBhMC\r\nQ04xMDAuBgNVBAoTJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhv\r\ncml0eTEXMBUGA1UEAxMOQ0ZDQSBURVNUIE9DQTEwHhcNMTcxMTAxMDcyNDA4WhcN\r\nMjAxMTAxMDcyNDA4WjB3MQswCQYDVQQGEwJjbjESMBAGA1UEChMJQ0ZDQSBPQ0Ex\r\nMQ4wDAYDVQQLEwVDVVBSQTEUMBIGA1UECxMLRW50ZXJwcmlzZXMxLjAsBgNVBAMU\r\nJTA0MUBaMjAxNy0xMS0xQDAwMDQwMDAwOlNJR05AMDAwMDAwMDEwggEiMA0GCSqG\r\nSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDIWO6AESrg+34HgbU9mSpgef0sl6avr1d\r\nbD/IjjZYM63SoQi3CZHZUyoyzBKodRzowJrwXmd+hCmdcIfavdvfwi6x+ptJNp9d\r\nEtpfEAnJk+4quriQFj1dNiv6uP8ARgn07UMhgdYB7D8aA1j77Yk1ROx7+LFeo7rZ\r\nDdde2U1opPxjIqOPqiPno78JMXpFn7LiGPXu75bwY2rYIGEEImnypgiYuW1vo9UO\r\nG47NMWTnsIdy68FquPSw5FKp5foL825GNX3oJSZui8d2UDkMLBasf06Jz0JKz5AV\r\nblaI+s24/iCfo8r+6WaCs8e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05LAwj5sdX2reWjzzAiKv1cr6ljN3J+cPpv00ibtHy4lTnok4ARhSAI/Zx3bHxv3T4yjJ2ce1OQ==","traceNo":"060626","traceTime":"0215100300","txnAmt":"1","txnSubType":"01","txnTime":"20190215100300","txnType":"01","version":"5.1.0"},"cookie":{},"data":"","headers":{"host":"61.163.79.140:9003","connection":"keep-alive","content-length":"2464","cache-control":"max-age=0","upgrade-insecure-requests":"1","origin":"null","content-type":"application/x-www-form-urlencoded","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate","accept-language":"zh-CN,zh;q=0.9"},"params":{},"query":{},"remote_address":"::ffff:192.168.40.206","url":"/union-payfinish-front","modelid":"custom::js_union-payfinish-front","spaceid":"jewelry_mall"}Props={"headers":{"actionid":"411f90e7-6892-4dac-8a0c-60adbf0862cb","messagetype":"dataservice.nodejs","sessionid":"308f95e3-3f09-4c45-bdd2-41afed55a19e","spaceid":"jewelry_mall"},"message_id":"7580f5ea-9089-4d30-9570-ac207fd588c1"}
	const spaceid = msg.spaceid;
	try {
		const data = msg.body as IUnionJSON;
		log('union front callback:');
		log(JSON.stringify(data));
		// {"accNo":"6226********0048","accessType":"0","bizType":"000201","currencyCode":"156","encoding":"UTF-8","exchangeRate":"0","merId":"777290058165389","orderId":"1550196180907","queryId":"641902151003000606268","respCode":"00","respMsg":"success","settleAmt":"1","settleCurrencyCode":"156","settleDate":"0215","signMethod":"01","signPubKeyCert":"-----BEGIN CERTIFICATE-----\r\nMIIEQzCCAyugAwIBAgIFEBJJZVgwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UEBhMC\r\nQ04xMDAuBgNVBAoTJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhv\r\ncml0eTEXMBUGA1UEAxMOQ0ZD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z6BUsLbIpp1ho5h+LaMnxMs6jdXXDh/du8X5SKMaIddiLw7ujZy1LibKy2jYi\r\nYYfs3tbZ0ffCKQtv78vCgC+IxUUurALY4w58fRLLdu8u8p9jyRFHsQEwSq+W5+bP\r\nMTh2w7cDd9h+6KoCN6AMI1Ly7MxRIhCbNBL9bzaxF9B5GK86ARY7ixkuDCEl4XCF\r\nJGxeoye9R46NqZ6AA/k97mJun//gmUjStmb9PUXA59fR5suAB5o/5lBySZ8UXkrI\r\npp/iLT8vIl1hNgLh0Ghs7DBSx99I+S3VuUzjHNxL6fGRhlix7Rb8\r\n-----END CERTIFICATE-----","signature":"UhcVbMCkDjUxlYTzXrFxHhU1EZVEhOdSC4uP2Eaxq6B1RiPQTBlacX5reSG8EZa2rL1ADVD0RoUr/Gkvy8MRvwON3L9xBigGHw4F58rTfRXEl56uSjt5y4zsz3oITnDdNezNWMhx+fNtvQgyaoI4PLhx2bU0TyHpmsZzLffRNY0FnH2stYFKmlDlD6esB636qIuOdUhizXl6nI8TbXPUapubj/m7WZl1LcZcXVCB8zSn6T3jK/sp9w5Y8hzEzkegvIYUImJx/XJ05LAwj5sdX2reWjzzAiKv1cr6ljN3J+cPpv00ibtHy4lTnok4ARhSAI/Zx3bHxv3T4yjJ2ce1OQ==","traceNo":"060626","traceTime":"0215100300","txnAmt":"1","txnSubType":"01","txnTime":"20190215100300","txnType":"01","version":"5.1.0"}
		// 逻辑处理
		const res = await pay_process(data, spaceid);
		// 根据返回的结果，重定`redirect`向到相关页面
		return { data: 'ok', redirect: 'http://www.daokes.com' };
	} catch (error) {
		const err_msg = (error && error.toString()) || 'unknown';
		// 银联支付回调失败
		return { data: err_msg, status_code: 500 };
	}
}
